import 'package:flutter/material.dart';
import 'package:ost_flutter_module/common/adapt.dart';
import 'package:ost_flutter_module/common/color.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_search_bar.dart';
import 'package:ost_flutter_module/widget/custom_widget/text_button.dart';

class GSearchAppBar extends StatefulWidget {
  final String hintText;
  final double borderRadius;
  final Function(String) onSearch;
  final Function(String) onChanged;
  final VoidCallback onClear;
  final VoidCallback onCancel;

  const GSearchAppBar(
      {Key key,
      this.hintText,
      this.borderRadius,
      this.onSearch,
      this.onChanged,
      this.onClear,
      this.onCancel})
      : super(key: key);

  @override
  _GSearchAppBarState createState() => _GSearchAppBarState();
}

class _GSearchAppBarState extends State<GSearchAppBar> {
  bool showCancel;

  @override
  void initState() {
    super.initState();
    showCancel = false;
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        BackButton(color: Colors.black),
        SizedBox(width: Adapt.px(8)),
        Expanded(
          child: GSearchBar(
            hintText: widget.hintText,
            borderRadius: widget.borderRadius,
            onSearch: (value) {
              setState(() {
                showCancel = value.isNotEmpty;
              });
              widget.onSearch(value);
            },
            onChanged: (value) {
              setState(() {
                showCancel = value.isNotEmpty;
              });
              widget.onChanged(value);
            },
            onClear: () => widget.onClear,
          ),
        ),
        SizedBox(width: Adapt.px(8)),
        showCancel
            ? GTextButton(
                title: '取消',
                fontSize: 17,
                normalForgroundColor: color303030,
                onPressed: widget.onCancel,
              )
            : Container(),
      ],
    );
  }
}
